/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tictactoe;

import java.awt.event.ActionEvent;
import javax.swing.JButton;

/**
 *
 * @author Jose Gabriel
 */
public class Jugada extends JButton{

    
    public Jugada() {
        this.setText("");
    }
    
    public void setJugada(int jugada){
        if(jugada==1){
            this.setText("X");
        }else{
            this.setText("O");
        }
    }
    
    public void iniciar(){
        this.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                evento(evt);
            }
        });
    }
    
    public void evento(ActionEvent event){
        if(TicTacToe.getTableroLogico().finJuego()){
            return;
        }
        
        int x= this.getActionCommand().charAt(0)-48;
        int y= this.getActionCommand().charAt(1)-48;
        
        if( TicTacToe.getTableroLogico().Jugar(x, y, TicTacToe.getJugadorActual()) ){
            TicTacToe.getGraficos().jugar(x, y);
            TicTacToe.setJugadorActual(TicTacToe.getJugadorActual()*-1);
            if ( TicTacToe.getTableroLogico().finJuego() ){
                TicTacToe.getGraficos().finDelJuego();
            }
        }else{
            TicTacToe.getGraficos().jugadaInvalida();
            /*try{
                Thread.sleep(2000);
            }catch(InterruptedException e){
                System.out.println("Se interrumpio el sleep");
            }
            TicTacToe.getGraficos().setTexto("");*/
            
        }
      
    }
    
}
